package com.ioftv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ioftv.entity.LabelerBoxRecord;
import com.ioftv.mapper.LabelerBoxRecordMapper;
import com.ioftv.service.LabelerBoxRecordService;
import com.ioftv.service.base.BaseService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * 标注员拉框记录Service实现类
 */
@Service
public class LabelerBoxRecordServiceImpl extends BaseService<LabelerBoxRecordMapper, LabelerBoxRecord> implements LabelerBoxRecordService {

    @Override
    public List<LabelerBoxRecord> getByRecordDate(LocalDate recordDate, Integer limit) {
        LambdaQueryWrapper<LabelerBoxRecord> queryWrapper = createUserQueryWrapper();
        queryWrapper.eq(LabelerBoxRecord::getRecordDate, recordDate);
        queryWrapper.orderByDesc(LabelerBoxRecord::getCreateTime);
        
        if (limit != null && limit > 0) {
            queryWrapper.last("LIMIT " + limit);
        }
        
        return this.list(queryWrapper);
    }
    
    @Override
    public List<LabelerBoxRecord> getByRecordDateAndUserId(LocalDate recordDate, Long userId, Integer limit) {
        LambdaQueryWrapper<LabelerBoxRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabelerBoxRecord::getRecordDate, recordDate);
        queryWrapper.eq(LabelerBoxRecord::getUserId, userId);
        queryWrapper.orderByDesc(LabelerBoxRecord::getCreateTime);
        
        if (limit != null && limit > 0) {
            queryWrapper.last("LIMIT " + limit);
        }
        
        return this.list(queryWrapper);
    }

    @Override
    public void batchUpdateStandard(LocalDate recordDate, Integer standardValue) {
        LambdaUpdateWrapper<LabelerBoxRecord> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(LabelerBoxRecord::getStandardValue, standardValue);
        updateWrapper.eq(LabelerBoxRecord::getRecordDate, recordDate);
        
        // 管理员可以更新所有数据，普通用户只能更新自己的数据
        if (!"ADMIN".equals(getCurrentUserRole())) {
            updateWrapper.eq(LabelerBoxRecord::getUserId, getCurrentUserId());
        }
        
        this.update(updateWrapper);
    }
    
    @Override
    public void batchUpdateStandardAndUserId(LocalDate recordDate, Integer standardValue, Long userId) {
        LambdaUpdateWrapper<LabelerBoxRecord> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(LabelerBoxRecord::getStandardValue, standardValue);
        updateWrapper.eq(LabelerBoxRecord::getRecordDate, recordDate);
        updateWrapper.eq(LabelerBoxRecord::getUserId, userId);
        
        this.update(updateWrapper);
    }

    @Override
    protected void addUserCondition(LambdaQueryWrapper<LabelerBoxRecord> queryWrapper) {
        queryWrapper.eq(LabelerBoxRecord::getUserId, getCurrentUserId());
    }

    @Override
    protected void setEntityUserId(LabelerBoxRecord entity) {
        if (entity.getUserId() == null) {
            entity.setUserId(getCurrentUserId());
        }
    }
}